package com.zcoj.security.config;

import com.zcoj.security.filter.JwtAuthenticationTokenFilter;
import com.zcoj.security.handler.CustomAuthenticationEntryPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@ComponentScan(basePackages = {"com.zcoj.api.*"})
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;



    @Autowired
    private CustomAuthenticationEntryPoint customAuthenticationEntryPoint;


    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http
                // CSRF禁用，因为不使用session
                .csrf().disable()
                // 不通过Session获取SecurityContext
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                // 过滤请求
                .authorizeRequests()
                // 对于登录接口 允许匿名访问
                .antMatchers("/user/login/emailCode"
                        ,"/user/login"
                        ,"/user/login/ticket"
                        ,"/user/registerUser"
                        , "/user/retrievePassword"
                        ,"/user/sendEmailCode/**"
                        ,"/user/adminLogin"
                        , "/swagger-ui.html"
                        , "/webjars/**"
                        , "/v2/**"
                        , "/swagger-resources/**").anonymous()
                .antMatchers("/home/**",
                        "/file/**",
                        "/filing/**",
                        "/rank/getRecentWeekRank",
                        "/announcement/pageVo",
                        "/system/**",
                        "/user/getImageBase64",
                        "/wx/qrCode/**/getQrCodeCreateTmpTicket/**",
                        "/wx/portal/**/").permitAll()
                //.antMatchers("/file/problem/testcase/**").hasAnyRole("super_admin","admin","problem_admin")
                // 除上面外的所有请求全部需要鉴权认证
                .anyRequest().authenticated()
                .and().httpBasic()
                .authenticationEntryPoint(customAuthenticationEntryPoint);

        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
        //允许跨域
        http.cors();


    }
    // 创建BCryptPasswordEncoder注入容器
    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }




}
